package com.wy.jfree_chart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.chart.ui.UIUtils;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class ExamReportCharts extends ApplicationFrame {

    public ExamReportCharts(String title) {
        super(title);
        setupUI();
    }

    private void setupUI() {
        // 创建主面板
        JPanel mainPanel = new JPanel(new GridLayout(2, 2, 10, 10));
        
        // 添加各个图表
        mainPanel.add(createScoreDistributionChart());
        mainPanel.add(createSubjectComparisonChart());
        mainPanel.add(createScoreTrendChart());
        mainPanel.add(createPassRateChart());
        
        setContentPane(mainPanel);
        pack();
        UIUtils.centerFrameOnScreen(this);
        setVisible(true);
    }

    // 1. 分数分布柱状图
    private ChartPanel createScoreDistributionChart() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        // 测试数据 - 分数段分布
        dataset.addValue(15, "人数", "0-59");
        dataset.addValue(45, "人数", "60-69");
        dataset.addValue(120, "人数", "70-79");
        dataset.addValue(85, "人数", "80-89");
        dataset.addValue(35, "人数", "90-100");

        JFreeChart chart = ChartFactory.createBarChart(
            "分数分布",       // 图表标题
            "分数段",         // 横轴标签
            "人数",           // 纵轴标签
            dataset,          // 数据集
            PlotOrientation.VERTICAL,
            true,             // 显示图例
            true,             // 显示工具提示
            false             // 不生成URL
        );

        // 自定义样式
        customizeBarChart(chart, new Color(79, 129, 189));
        
        return new ChartPanel(chart);
    }

    // 2. 科目成绩对比柱状图
    private ChartPanel createSubjectComparisonChart() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        
        // 测试数据 - 各科目平均分
        dataset.addValue(85, "平均分", "语文");
        dataset.addValue(78, "平均分", "数学");
        dataset.addValue(92, "平均分", "英语");
        dataset.addValue(76, "平均分", "物理");
        dataset.addValue(88, "平均分", "化学");
        dataset.addValue(81, "平均分", "生物");

        JFreeChart chart = ChartFactory.createBarChart(
            "各科目成绩对比",
            "科目",
            "分数",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );

        // 使用不同颜色
        customizeBarChart(chart, new Color(155, 187, 89));
        
        return new ChartPanel(chart);
    }

    // 3. 成绩趋势折线图
    private ChartPanel createScoreTrendChart() {
        XYSeries series = new XYSeries("平均分趋势");
        
        // 测试数据 - 历次考试平均分
        series.add(1, 72);
        series.add(2, 75);
        series.add(3, 78);
        series.add(4, 82);
        series.add(5, 85);
        series.add(6, 83);
        series.add(7, 87);

        XYSeriesCollection dataset = new XYSeriesCollection(series);
        
        JFreeChart chart = ChartFactory.createXYLineChart(
            "成绩趋势分析",
            "考试次数",
            "平均分",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );

        // 自定义折线图样式
        XYPlot plot = chart.getXYPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesPaint(0, new Color(255, 128, 64));
        renderer.setSeriesStroke(0, new BasicStroke(2.5f));
        renderer.setSeriesShapesVisible(0, true);
        
        plot.setRenderer(renderer);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
        
        return new ChartPanel(chart);
    }

    // 4. 通过率饼图
    private ChartPanel createPassRateChart() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        
        // 测试数据
        dataset.setValue("优秀 (90-100)", 35);
        dataset.setValue("良好 (80-89)", 85);
        dataset.setValue("及格 (60-79)", 165);
        dataset.setValue("不及格 (0-59)", 15);

        JFreeChart chart = ChartFactory.createPieChart(
            "成绩等级分布",
            dataset,
            true,
            true,
            false
        );

        // 自定义饼图样式
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setSectionPaint("优秀 (90-100)", new Color(155, 187, 89));
        plot.setSectionPaint("良好 (80-89)", new Color(79, 129, 189));
        plot.setSectionPaint("及格 (60-79)", new Color(255, 128, 64));
        plot.setSectionPaint("不及格 (0-59)", new Color(192, 80, 77));
        
        plot.setBackgroundPaint(Color.WHITE);
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        
        return new ChartPanel(chart);
    }

    // 自定义柱状图样式
    private void customizeBarChart(JFreeChart chart, Color barColor) {
        CategoryPlot plot = chart.getCategoryPlot();
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        
        // 设置柱状图颜色
        renderer.setSeriesPaint(0, barColor);
        
        // 使用现代样式的柱状图
        renderer.setBarPainter(new StandardBarPainter());
        renderer.setDrawBarOutline(false);
        renderer.setShadowVisible(false);
        
        // 设置背景和网格线
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
    }

    // 保存图表为图片文件
    public void saveCharts() {
        try {
            JPanel panel = (JPanel) getContentPane().getComponent(0);
            
            for (int i = 0; i < panel.getComponentCount(); i++) {
                Component comp = panel.getComponent(i);
                if (comp instanceof ChartPanel) {
                    ChartPanel chartPanel = (ChartPanel) comp;
                    JFreeChart chart = chartPanel.getChart();
                    ChartUtils.saveChartAsPNG(new File("chart_" + i + ".png"), chart, 600, 400);
                }
            }
            System.out.println("图表已保存为PNG文件");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 设置系统样式
        try {
            UIManager.setLookAndFeel(UIManager.getLookAndFeel());
        } catch (Exception e) {
            e.printStackTrace();
        }

        ExamReportCharts demo = new ExamReportCharts("考试报告图表");
        
        // 可选：保存图表为图片
        demo.saveCharts();
    }
}